<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>图书借阅</title>
    <link rel="stylesheet" href="/static/css/bootstrap.min.css">
    <link rel="stylesheet" href="/static/css/dsb.css">
    <!-- 引入 ECharts 库 -->
    <script src="https://cdn.jsdelivr.net/npm/echarts@5.4.3/dist/echarts.min.js"></script>
</head>
<body>

<nav class="navbar navbar-inverse navbar-fixed-top">
    <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar"
                    aria-expanded="false" aria-controls="navbar">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand one-pan-link-mark" href="/logout/">退出登陆</a>
        </div>
    </div>
</nav>

<div class="container-fluid">
    <div class="row">
        <div class="col-sm-3 col-md-2 sidebar">
            <ul class="nav nav-sidebar">
                <li class=""><a href="/index" class="one-pan-link-mark">基本信息</a></li>
                <li class=""><a href="/course" class="one-pan-link-mark">选课信息</a></li>
                <li class=""><a href="/mark" class="one-pan-link-mark">成绩信息</a></li>
                <li class=""><a href="/money" class="one-pan-link-mark">消费信息</a></li>
                <li class="active"><a href="/book" class="one-pan-link-mark">图书借阅</a></li>
                <li class=""><a href="/all_books/" class="one-pan-link-mark">图书管理</a></li>
                <li class=""><a href="/course_manage" class="one-pan-link-mark">课程管理</a></li>
            </ul>
        </div>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <div class="d-flex justify-content-between align-items-center">
                <h2 class="sub-header" style="display: inline-block; margin-right: 20px;">图书借阅信息</h2>
                <div style="display: inline-block;">
                    <a href="/all_books/" class="btn btn-success">借书</a>
                </div>
            </div>
            
            {% if success_message %}
            <div class="alert alert-success">
                {{ success_message }}
            </div>
            {% endif %}
            
            {% if error_message %}
            <div class="alert alert-danger">
                {{ error_message }}
            </div>
            {% endif %}
            
            <div class="table-responsive">
                <table class="table table-striped">
                    <thead>
                    <tr>
                        <th>序号</th>
                        <th>书名</th>
                        <th>借阅时间</th>
                        <th>归还时间</th>
                        <th>状态</th>
                    </tr>
                    </thead>
                    <tbody>
                    {% if lends %}
                        {% for lend in lends %}
                        <tr>
                            <td>{{ forloop.counter }}</td>
                            <td>{{ lend.le_bo.bo_name }}</td>
                            <td>{{ lend.le_start|date:"Y-m-d H:i:s" }}</td>
                            <td>
                                {% if lend.le_start|date:"YmdHis" == lend.le_end|date:"YmdHis" %}
                                    -
                                {% else %}
                                    {{ lend.le_end|date:"Y-m-d H:i:s" }}
                                {% endif %}
                            </td>
                            <td>
                                {% if lend.le_start|date:"YmdHis" == lend.le_end|date:"YmdHis" %}
                                    <span class="label label-warning">借阅中</span>
                                    <a href="/return_book/{{ lend.le_bo.bo_num }}/" class="btn btn-xs btn-primary">归还</a>
                                {% else %}
                                    <span class="label label-success">已归还</span>
                                {% endif %}
                            </td>
                        </tr>
                        {% endfor %}
                    {% else %}
                        <tr>
                            <td colspan="5" class="text-center">暂无借阅记录</td>
                        </tr>
                    {% endif %}
                    </tbody>
                </table>
            </div>
            
            <!-- 添加图书借阅统计图表区域 -->
            <div class="panel panel-default">
                <div class="panel-heading">图书借阅统计</div>
                <div class="panel-body">
                    <div id="bookChart" style="width: 100%; height: 400px;"></div>
                </div>
            </div>
            
            {% if lends %}
            <div class="panel panel-default">
                <div class="panel-heading">借书统计</div>
                <div class="panel-body">
                    <p>总借阅次数：{{ lends|length }}次</p>
                </div>
            </div>
            {% endif %}
        </div>
    </div>
</div>

</body>
<script>
    // 获取所有的提示消息
    var alerts = document.querySelectorAll('.alert');
    
    // 为每个提示消息设置3秒后自动隐藏
    alerts.forEach(function(alert) {
        setTimeout(function() {
            alert.style.transition = 'opacity 1s';
            alert.style.opacity = '0';
            setTimeout(function() {
                alert.style.display = 'none';
            }, 1000);
        }, 3000);
    });
    
    // 初始化图书借阅统计图表
    document.addEventListener('DOMContentLoaded', function() {
        // 初始化ECharts实例
        var bookChart = echarts.init(document.getElementById('bookChart'));
        
        // 显示加载动画
        bookChart.showLoading();
        
        // 从后端API获取数据
        fetch('/book/chart/')
            .then(response => response.json())
            .then(data => {
                // 隐藏加载动画
                bookChart.hideLoading();
                
                if (data.code === 200) {
                    // 使用返回的数据设置图表选项
                    var option = {
                        title: {
                            text: '图书借阅次数排行'
                        },
                        tooltip: {
                            trigger: 'axis',
                            axisPointer: {
                                type: 'shadow'
                            }
                        },
                        legend: {
                            data: ['借阅次数']
                        },
                        grid: {
                            left: '3%',
                            right: '4%',
                            bottom: '3%',
                            containLabel: true
                        },
                        xAxis: data.data.xAxis,
                        yAxis: data.data.yAxis,
                        series: data.data.series
                    };
                    
                    // 使用配置项显示图表
                    bookChart.setOption(option);
                } else {
                    // 如果没有数据，显示提示信息
                    document.getElementById('bookChart').innerHTML = '<div class="alert alert-info text-center">暂无图书借阅统计数据</div>';
                }
            })
            .catch(error => {
                // 隐藏加载动画
                bookChart.hideLoading();
                console.error('获取图表数据失败:', error);
                document.getElementById('bookChart').innerHTML = '<div class="alert alert-danger text-center">获取统计数据失败</div>';
            });
            
        // 窗口大小改变时，重置图表大小
        window.addEventListener('resize', function() {
            bookChart.resize();
        });
    });
</script>
</html>